package com.xhj.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xhj.client.OrderClient;
import com.xhj.config.CourseQuery;
import com.xhj.entity.Course;
import com.xhj.entity.CourseInfoForm;
import com.xhj.service.ChapterService;
import com.xhj.service.CourseService;
import com.xhj.util.JwtUtil;
import com.xhj.util.ResultEntity;
import com.xhj.vo.ChapterVo;
import com.xhj.vo.CourseInfoVo;
import com.xhj.vo.CoursePublishVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value="课程管理")
@RestController
@CrossOrigin
@RequestMapping("/EduAdmin/course/")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private ChapterService chapterService;
    
    @Autowired
    private OrderClient orderClient;

    @ApiOperation(value = "新增课程")
    @RequestMapping("addCourse")
    public ResultEntity addCourse(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm){
        String courseId = courseService.saveCourse(courseInfoForm);
        if (!StringUtils.isEmpty(courseId)) {
            return ResultEntity.successWithData().data("courseId",courseId);
        }
        return ResultEntity.failedWithMessage("保存失败!");
    }

    @ApiOperation(value = "根据Id查询课程")
    @RequestMapping("info/{id}")
    public ResultEntity getInfoById(
            @ApiParam(name = "id", value = "课程Id", required = true)
            @PathVariable("id") String id){

        CourseInfoForm courseInfoForm = courseService.getCourseInfoFormById(id);
        return ResultEntity.successWithData().data("courseInfo",courseInfoForm);
    }

    @ApiOperation(value = "根据Id查询课程")
    @RequestMapping("courseInfo/{id}")
    public com.xhj.vo.CourseInfoForm getCourseInfoVoById(
            @ApiParam(name = "id", value = "课程Id", required = true)
            @PathVariable("id") String id){

        CourseInfoForm courseInfoForm = courseService.getCourseInfoFormById(id);
        com.xhj.vo.CourseInfoForm courseInfoForm1 = new com.xhj.vo.CourseInfoForm();
        BeanUtils.copyProperties(courseInfoForm,courseInfoForm1);
        return courseInfoForm1;
    }

    @ApiOperation(value = "更新课程信息")
    @RequestMapping("update/{id}")
    public ResultEntity updateCourseById(
            @ApiParam(name = "courseInfoForm", value = "课程基本信息", required = true)
            @RequestBody CourseInfoForm courseInfoForm,
            @ApiParam(name = "id", value = "课程Id", required = true)
            @PathVariable String id){
        courseService.updateCourseById(courseInfoForm);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "根据Id获取课程发布信息")
    @RequestMapping("coursePublish/{id}")
    public ResultEntity getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程Id", required = true)
            @PathVariable String id){
        CoursePublishVo coursePublishVo = courseService.getCoursePublishVoById(id);
        return ResultEntity.successWithData().data("coursePublishVo",coursePublishVo);
    }

    @ApiOperation(value = "根据Id发布课程")
    @RequestMapping("publish/{id}")
    public ResultEntity publishCourseById(
            @ApiParam(name = "id", value = "课程Id", required = true)
            @PathVariable String id){
        courseService.publishCourseById(id);
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "获取课程列表")
    @RequestMapping("list/{page}/{limit}")
    public ResultEntity getCourseList(
            @ApiParam(name = "page",value = "当前页码",required = true)
            @PathVariable("page") Integer page,
            @ApiParam(name = "limit",value = "每页记录数",required = true)
            @PathVariable("limit") Integer limit,
            @ApiParam(name = "courseQuery",value = "课程查询条件对象")
            @RequestBody(required = false) CourseQuery courseQuery
            ){
        Page<Course> pageParam = new Page<>(page, limit);
        IPage<Course> coursePage = courseService.getListByCourseQuery(pageParam,courseQuery);
        List<Course> courseList = coursePage.getRecords();
        long total = coursePage.getTotal();
        return ResultEntity.successWithData()
                .data("list",courseList)
                .data("total",total)
                .data("size",coursePage.getSize())
                .data("pages",coursePage.getPages())
                .data("current",coursePage.getCurrent());
    }

    @ApiOperation(value = "根据Id删除课程")
    @RequestMapping("delete/{id}")
    public ResultEntity deleteCourseById(
            @ApiParam(name = "id", value = "课程Id", required = true)
            @PathVariable String id){
        boolean result = courseService.deleteCourseById(id);
        if (!result) return ResultEntity.failedWithMessage("删除课程失败！");
        return ResultEntity.successWithoutData();
    }

    @ApiOperation(value = "根据Id查询课程信息")
    @RequestMapping("get/{id}")
    public ResultEntity getCourseInfoById(
            @ApiParam(name = "id", value = "课程Id", required = true)
            @PathVariable("id") String id, HttpServletRequest request){

        CourseInfoVo courseInfoVo = courseService.getCourseInfoById(id);
        List<ChapterVo> chapterList = chapterService.chapterList(id);
        boolean isBuyCourse = orderClient.isBuyCourse(JwtUtil.getMemberIdByJwtToken(request), id);

        return ResultEntity.successWithData()
                .data("courseInfo",courseInfoVo)
                .data("chapterList",chapterList)
                .data("isBuyCourse",isBuyCourse);
    }

    @ApiOperation(value = "根据Id查询课程信息")
    @RequestMapping("update/viewCount/{id}")
    public ResultEntity updateViewCount(
            @ApiParam(name = "id", value = "课程Id", required = true)
            @PathVariable("id") String id){
        courseService.updatePageViewCount(id);
        return ResultEntity.successWithoutData();
    }

}
